﻿using System;
using System.Collections.Generic;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;

namespace Zeus.Components.Nopi.Excel
{
    public class ExcelFormatHelper
    {
        // write an empty row
        public static Row WriteEmptyRow(Sheet sheet, ref int rowNum, bool isFirstRow)
        {
            if (!isFirstRow)
                rowNum++;

            return sheet.CreateRow(rowNum);
        }

        // writer template title
        public static void WriterTitle(Sheet sheet, ref int rowNum, int colNum, string title,
            Font font, CellStyle cellStyle, TemplateSetting templateSetting)
        {
            Cell currentCell;
            Row currentRow = WriteEmptyRow(sheet, ref rowNum, false);

            currentCell = currentRow.CreateCell(1);
            currentCell.SetCellValue(title);  
 
            // set title cell style
            currentCell.Row.Height = templateSetting.TitleRowHeight;
            font.Boldweight = (short)FontBoldWeight.BOLD;
            font.FontHeightInPoints = templateSetting.TitleFontSize;
            cellStyle.SetFont(font);
            currentCell.CellStyle = cellStyle;
        }

        // write template header
        public static void WriterHeader(Sheet sheet, ref int rowNum, int colNum, Dictionary<string, string> headDict)
        {
            Cell cell;
            Row currentRow;

            foreach (KeyValuePair<string, string> headElem in headDict)
            {
                rowNum++;
                currentRow = sheet.CreateRow(rowNum);

                cell = currentRow.CreateCell(colNum);
                cell.SetCellValue(headElem.Key);
                
                cell = currentRow.CreateCell(colNum + 1);
                cell.SetCellValue(headElem.Value); 
            }
        }

        // write template footer
        public static void WriterFooter(Sheet sheet, ref int rowNum, int colNum, List<string> footList)
        {
            Cell cell;
            Row currentRow;

            foreach (string footElem in footList)
            {
                rowNum++;
                currentRow = sheet.CreateRow(rowNum);

                cell = currentRow.CreateCell(colNum);
                cell.SetCellValue(footElem);
            }
        }

        // format table head cell
        public static void FormatTableHead(Cell cell, CellStyle cellStyle, Font font, HSSFPalette palette, TemplateSetting templateSetting)
        {
            // cell.Row.Height = TemplateConstants.TableHeaderRowHeight;
            cell.Row.Height = 500;
            SetCellColor(cellStyle, palette, true, templateSetting);
                    
            font.Boldweight = (short)FontBoldWeight.BOLD;
            cellStyle.SetFont(font);

            cellStyle.WrapText = true;
            cellStyle.Alignment = HorizontalAlignment.CENTER;
            cellStyle.VerticalAlignment = VerticalAlignment.CENTER;

            cellStyle.BorderTop = CellBorderType.THICK;
            cellStyle.BorderBottom = CellBorderType.THICK;
            cellStyle.BorderLeft = CellBorderType.THICK;
            cellStyle.BorderRight = CellBorderType.THICK;

            cell.CellStyle = cellStyle;
        }

        // format table head cell
        public static void FormatTableBody(Cell cell, CellStyle cellStyle, Font font, HSSFPalette palette, TemplateSetting templateSetting)
        {
            // cell.Row.Height = TemplateConstants.TableHeaderRowHeight;
            cell.Row.Height = 400;
            SetCellColor(cellStyle, palette, true, templateSetting);

            font.Boldweight = (short)FontBoldWeight.NORMAL;
            cellStyle.SetFont(font);

            cellStyle.WrapText = true;
            cellStyle.Alignment = HorizontalAlignment.CENTER;
            cellStyle.VerticalAlignment = VerticalAlignment.CENTER;

            cellStyle.BorderTop = CellBorderType.THIN;
            cellStyle.BorderBottom = CellBorderType.THIN;
            cellStyle.BorderLeft = CellBorderType.THIN;
            cellStyle.BorderRight = CellBorderType.THIN;

            cell.CellStyle = cellStyle;
        }

        private static void SetCellColor(CellStyle cellStyle, HSSFPalette palette, bool isTableHeader, TemplateSetting templateSetting)
        {
            cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            if (isTableHeader)
            {
                cellStyle.FillForegroundColor = HSSFColor.SKY_BLUE.index;

                // Set custom color
                palette.SetColorAtIndex(HSSFColor.SKY_BLUE.index, templateSetting.TableBodyColor.R, 
                    templateSetting.TableHeaderColor.G, templateSetting.TableHeaderColor.B);
            }
            else
            {
                cellStyle.FillForegroundColor = HSSFColor.ROYAL_BLUE.index;

                // Set custom color
                palette.SetColorAtIndex(HSSFColor.ROYAL_BLUE.index, templateSetting.TableBodyColor.R,
                    templateSetting.TableBodyColor.G, templateSetting.TableBodyColor.B);
            }
        }
    }
}
